package com.ddd.application.impl;

import com.ddd.application.dto.MemberPriceDto;
import com.ddd.application.dto.PriceDto;
import com.ddd.application.dto.StockDto;
import com.ddd.application.service.*;
import com.ddd.domain.model.product.ProductRepository;
import com.ddd.domain.model.sales.*;
import com.ddd.domain.model.stock.StockRepository;
import com.ddd.domain.share.TrackingId;
import com.ddd.interfaces.sales.facade.dto.SalesProductDto;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;


@Service
public class SalesServiceImpl implements SalesApplicationService {

    /**
     *
     */
    @Resource
    private ProductRepository productRepository;

    @Resource
    private StockRepository stockRepository;
    /**
     * 销售单号
     */
    private TrackingId trackingId;
    /**
     * 远程rpc访问
     */
    private StockServiceRpc stockServiceRpc;
    /**
     *
     */
    private PriceServiceRpc priceServiceRpc;
    /**
     *
     */
    private MemberServiceRpc memberServiceRpc;
    /**
     *
     */
    private PayServiceRpc payServiceRpc;
    /**
     *
     */
    private SalesService salesService;


    /**
     * @param salesProductDto
     * @return
     */
    @Override
    public boolean sales(final SalesProductDto salesProductDto) {
        //1,获取库存
        StockDto stockDto = stockServiceRpc.find(salesProductDto.getSkuId());
        //2,获取零售价
        PriceDto price = priceServiceRpc.findPrice(salesProductDto.getSkuId());
        //3,获取会员价，最终支付价格
        MemberPriceDto memberPrice = memberServiceRpc.findMemberPrice(salesProductDto.getSkuId(),price.getPrice());
        //销售，生成销售记录（销售的领域服务），扣减库存
        SalesProductValueObject salesProductValueObject = new SalesProductValueObject(
                salesProductDto.getSkuId(),
                salesProductDto.getCommonName(),
                salesProductDto.getBatchNo(),
                salesProductDto.getProductionDate(),
                salesProductDto.getExpiryDate(),
                stockDto.getNum(),
                memberPrice.getMemberPrice());
        String orderId = UUID.randomUUID().toString();
        CashierValueObject cashierValueObject;
        //pos机
        private PosValueObject posValueObject;
        SalesEntity salesEntity = new SalesEntity(orderId, salesProductValueObject);
        // 销售聚合，保存销售单据
        //4,开始支付
        boolean pay = payServiceRpc.pay(0, memberPrice.getMemberPrice());
        salesService.sales(salesEntity,pay);
        return false;
    }
}
